package com.xbai.goods.controller;

import com.github.pagehelper.PageInfo;
import com.xbai.entity.Result;
import com.xbai.goods.pojo.Sku;
import com.xbai.goods.service.SkuService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @desc SkuController
 * @author jxbai
 * @date 2021/1/28
 */

@RestController
@RequestMapping("/sku")
@CrossOrigin
@RequiredArgsConstructor
public class SkuController {

    private final SkuService skuService;

    @GetMapping("/status/{status}")
    public Result<List<Sku>> findByStatus(@PathVariable String status) {
        List<Sku> list = skuService.findByStatus(status);
        return new Result<>(list);
    }

    /***
     * Sku分页条件搜索实现
     * @param sku {@link Sku}
     * @param page 页码
     * @param size 页大小
     * @return {@link Result<PageInfo>}
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) Sku sku, @PathVariable  int page, @PathVariable  int size){
        //调用SkuService实现分页条件查询Sku
        PageInfo<Sku> pageInfo = skuService.findPage(sku, page, size);
        return new Result<>(pageInfo);
    }

    /***
     * Sku分页搜索实现
     * @param page 页码
     * @param size 页大小
     * @return {@link Result<PageInfo>}
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用SkuService实现分页查询Sku
        PageInfo<Sku> pageInfo = skuService.findPage(page, size);
        return new Result<>(pageInfo);
    }

    /***
     * 多条件搜索Sku数据
     * @param sku {@link Sku}
     * @return {@link Result}
     */
    @PostMapping(value = "/search" )
    public Result<List<Sku>> findList(@RequestBody(required = false) Sku sku){
        //调用SkuService实现条件查询Sku
        List<Sku> list = skuService.findList(sku);
        return new Result<>(list);
    }

    /***
     * 根据ID删除Sku数据
     * @param id 主键id
     * @return {@link Result}
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Long id){
        //调用SkuService实现根据主键删除
        skuService.delete(id);
        return new Result();
    }

    /***
     * 修改Sku数据
     * @param sku {@link Sku}
     * @param id 主键id
     * @return {@link Result}
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody Sku sku, @PathVariable Long id){
        //设置主键值
        sku.setId(id);
        //调用SkuService实现修改Sku
        skuService.update(sku);
        return new Result();
    }

    /***
     * 新增Sku数据
     * @param sku {@link Sku}
     * @return {@link Result}
     */
    @PostMapping
    public Result add(@RequestBody Sku sku){
        //调用SkuService实现添加Sku
        skuService.add(sku);
        return new Result();
    }

    /***
     * 根据ID查询Sku数据
     * @param id 主键id
     * @return {@link Result<Sku>}
     */
    @GetMapping("/{id}")
    public Result<Sku> findById(@PathVariable Long id){
        //调用SkuService实现根据主键查询Sku
        Sku sku = skuService.findById(id);
        return new Result<>(sku);
    }

    /***
     * 查询Sku全部数据
     * @return {@link Result}
     */
    @GetMapping
    public Result<List<Sku>> findAll(){
        //调用SkuService实现查询所有Sku
        List<Sku> list = skuService.findAll();
        return new Result<>(list) ;
    }
}
